﻿using DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.Controls
{
    public partial class ctAddNewArticle : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var proxypostcat = new tbPostsCat_Proxy();
                var _articleid = Request.QueryString["ArticleID"];
                int orgid = 1;

                if (_articleid != null)
                {
                    var proxypost = new tbPosts_Proxy();
                    var post = proxypost.GetById_tbPosts_Proxy(int.Parse(_articleid)).ToObject<tbPosts>();
                    btnDangBai.Value = "Lưu";
                    if (post.ImagePath != null)
                        imgHinhMinhHoa.ImageUrl = post.ImagePath;
                    orgid = post.OrgID;
                    if (post != null)
                    {
                        if (post.TypePosts == 2)
                        {
                            typeThongBao.Checked = true;
                            cbChuyenMuc.Enabled = false;
                        }
                        else
                            typeTinTuc.Checked = true;

                        var _orgid = Request.QueryString["OrgID"];
                        if (_orgid != null && post.OrgID.ToString() != _orgid)
                        {
                            Response.Redirect("~/404.html");
                            return;
                        }
                        else
                        {
                            cbChuyenMuc.DataSource = proxypostcat.GetByOrgID_tbPostsCat_Proxy(orgid);
                            cbChuyenMuc.DataTextField = "PostsCatName";
                            cbChuyenMuc.DataValueField = "PostsCatID";
                            if (post.PostsCatID != null)
                                cbChuyenMuc.SelectedValue = post.PostsCatID.ToString();
                            cbChuyenMuc.DataBind();
                        }
                        txtTieuDe.Value = post.Title;
                        txtNoiDung.Text = HttpUtility.HtmlDecode(post.Content);
                        txtVanTat.Text = HttpUtility.HtmlDecode(post.Intro);
                    }
                    else
                        Response.Redirect("~/404.html");
                }
                else
                {
                    var _orgid = Request.QueryString["OrgID"];
                    if (_orgid != null)
                        orgid = int.Parse(_orgid);
                    typeTinTuc.Checked = true;
                    cbChuyenMuc.DataSource = proxypostcat.GetByOrgID_tbPostsCat_Proxy(orgid);
                    cbChuyenMuc.DataTextField = "PostsCatName";
                    cbChuyenMuc.DataValueField = "PostsCatID";
                    cbChuyenMuc.DataBind();
                }
            }

            //            txtNoiDung.config.toolbar = new object[]
            //            {                
            //                new object[] { "Source", "-", "Save", "NewPage", "Preview", "-", "Templates" },
            //    new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" },
            //    new object[] { "Undo", "Redo", "-", "Find", "Replace", "-", "SelectAll", "RemoveFormat" },
            //    new object[] { "Form", "Checkbox", "Radio", "TextField", "Textarea", "Select", "Button", "ImageButton", "HiddenField" },
            //    "/",
            //    new object[] { "Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" },
            //    new object[] { "NumberedList", "BulletedList", "-", "Outdent", "Indent", "Blockquote", "CreateDiv" },
            //    new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
            //    new object[] { "BidiLtr", "BidiRtl" },
            //    new object[] { "Link", "Unlink", "Anchor" },
            //    new object[] { "Image", "Flash", "Table", "HorizontalRule", "Smiley", "SpecialChar", "PageBreak", "Iframe" },
            //    "/",
            //    new object[] { "Styles", "Format", "Font", "FontSize" },
            //    new object[] { "TextColor", "BGColor" },
            //    new object[] { "Maximize", "ShowBlocks", "-", "About" }
            //};

            //CKEditor1.CKEditorInstanceEventHandler = new System.Collections.Generic.List<object>();
            //CKEditor1.CKEditorInstanceEventHandler.Add(new object[] { "instanceReady", "function (evt) { alert('Event Handler attached on CKEditorInstanceEventHandler to editor: ' + evt.editor.name);}" });
            //            CKEditor1.CKEditorInstanceEventHandler.Add(new object[] { "instanceReady"});
            //, "function (evt) { alert('Event Handler attached on CKEditorInstanceEventHandler to editor: ' + evt.editor.name);}" });

        }
    }
}